package com.hjm.common.utils.request;

/**
 * 类名:HttpUtil
 * 描述：
 * 作者：luran
 * 时间：2019/6/11 10:18
 * 版本：1.0
 **/

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import org.dom4j.DocumentException;

import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;

/**
 * http请求工具类
 * @author liuxiangqin
 * 2015-09-01
 */
@SuppressWarnings("all")
public class HttpUtil {

    /**
     * 查询风险信息
     * @param docType
     * @param docCode
     * @return
     * @throws DocumentException
     */
    public static Map getRiskInfo(String docType, String docCode)  throws DocumentException {
        try{
            Map<String,String> req = new HashMap<String, String>();
            String rpid = ("UPLM"+System.currentTimeMillis()).substring(0, 15);
            req.put("rpid", rpid);
            req.put("funCode","EnMerQuery");
            req.put("docType",docType);//证件类型
            req.put("docCode",docCode);//证件号码
            Map result = postByXStream(AutoLoadData.ldqsxhfxxxurl,req);
            if(result != null && "0000".equals(result.get("retCode"))){
                //解析XML
                Map map = ParseXmlByPropertyFile.parseXml(String.valueOf(result.get("retMsg")));
                Map document = (Map) map.get("Document");
                Map respone = (Map) document.get("Respone");
                Map body = (Map) respone.get("Body");
                return body;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    public static Map<String, Object> postByXStream(String urlstr, Map<String, String> request) {
        XStream xstream = new XStream(new DomDriver());
        HttpURLConnection conn = null;
        InputStreamReader reader = null;
        Map<String, Object> obj = null;
        try {
            URL url = new URL(urlstr);
            conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(6 * 60 * 1000);
            conn.setReadTimeout(10 * 60 * 1000);
            conn.setRequestMethod("POST");
            if (request != null) {
                conn.setDoOutput(true);
                conn.getOutputStream().write(xstream.toXML(request).getBytes("UTF-8"));
                conn.getOutputStream().flush();
                conn.getOutputStream().close();
            }
            int rCode = conn.getResponseCode();
            if (rCode == HttpURLConnection.HTTP_OK) {
                if (conn.getInputStream() != null) {
                    reader = new InputStreamReader(conn.getInputStream(),"UTF-8");
                    xstream.alias("map", HashMap.class);
                    obj = (Map<String, Object>) xstream.fromXML(reader);
                }
            } else {
                obj = null;
            }
        } catch (Exception e) {
            obj = null;
        } finally {
            try {
                reader.close();
                conn.disconnect();
            } catch (Exception e) {
            }
        }
        return obj;
    }

    public static List<Map<String, Object>> getHead(Map map){
        List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
        List<Map> listmap= new ArrayList<>();
        List<String> nodeList = new ArrayList<String>();
        nodeList.add("Respone>Head>SecretKey");
        nodeList.add("Respone>Head>OrigSender");
        nodeList.add("Respone>Head>Version");
        nodeList.add("Respone>Head>TrnxTime");
        nodeList.add("Respone>Head>OrigSenderSID");
        nodeList.add("Respone>Head>Identification");
        nodeList.add("Respone>Head>TrnxCode");
        nodeList.add("Respone>Head>RecSystemId");
//        resultList=ParseXmlByPropertyFile.str2ListbyHandler(nodeList,map.get("retMsg").toString());
        return  resultList;
    }

    public static List<Map<String, Object>> QueryInfo(Map map){
        List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
        List<String> nodeList = new ArrayList<String>();
        nodeList.add("Respone>Body>QueryInfo>DocCode");
        nodeList.add("Respone>Body>QueryInfo>LegDocName");
        nodeList.add("Respone>Body>QueryInfo>ResultSequence");
        nodeList.add("Respone>Body>QueryInfo>RegName");
        nodeList.add("Respone>Body>QueryInfo>LegDocCode");
        nodeList.add("Respone>Body>QueryInfo>DocType");
//        resultList=ParseXmlByPropertyFile.str2ListbyHandler(nodeList,map.get("retMsg").toString());
        return  resultList;
    }

    public static List<Map<String, Object>> CurSignList(Map map){
        List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();

        List<String> Statuslist = new ArrayList<String>();
        LinkedList<Map<String, Object>> Status = new LinkedList<>();
        Statuslist.add("Respone>Body>ResultInfo>CurSignList>SignInfo>Status");
//        Status=ParseXmlByPropertyFile.str2ListbyHandler(Statuslist,map.get("retMsg").toString());

        List<String> OpenTypelist = new ArrayList<String>();
        LinkedList<Map<String, Object>> OpenType =  new LinkedList<>();
        OpenTypelist.add("Respone>Body>ResultInfo>CurSignList>SignInfo>OpenType");
//        OpenType=ParseXmlByPropertyFile.str2ListbyHandler(OpenTypelist,map.get("retMsg").toString());

        List<String> EndTimelist = new ArrayList<String>();
        EndTimelist.add("Respone>Body>ResultInfo>CurSignList>SignInfo>EndTime");
        LinkedList<Map<String, Object>> EndTime =  new LinkedList<>();
//        EndTime=ParseXmlByPropertyFile.str2ListbyHandler(EndTimelist,map.get("retMsg").toString());


        List<String> StartTimelist = new ArrayList<String>();
        LinkedList<Map<String, Object>> StartTime =  new LinkedList<>();
        StartTimelist.add("Respone>Body>ResultInfo>CurSignList>SignInfo>StartTime");
//        StartTime=ParseXmlByPropertyFile.str2ListbyHandler(StartTimelist,map.get("retMsg").toString());

        List<String> OrgNamelist = new ArrayList<String>();
        OrgNamelist.add("Respone>Body>ResultInfo>CurSignList>SignInfo>OrgName");
        LinkedList<Map<String, Object>> OrgName = new LinkedList<>();
//        OrgName=ParseXmlByPropertyFile.str2ListbyHandler(OrgNamelist,map.get("retMsg").toString());


        List<String> RiskStatuslist = new ArrayList<String>();
        RiskStatuslist.add("Respone>Body>ResultInfo>CurSignList>SignInfo>RiskStatus");
        LinkedList<Map<String, Object>> RiskStatus =  new LinkedList<>();
//        RiskStatus=ParseXmlByPropertyFile.str2ListbyHandler(RiskStatuslist,map.get("retMsg").toString());


        for (int i=0;i<Status.size();i++){
            Map temp = new HashMap();
            temp.put("Status",Status.get(i).get("Status"));
            temp.put("OpenType",OpenType.get(i).get("OpenType"));
            temp.put("EndTime",EndTime.get(i).get("EndTime"));
            temp.put("StartTime",StartTime.get(i).get("StartTime"));
            temp.put("OrgName",OrgName.get(i).get("OrgName"));
            temp.put("RiskStatus",RiskStatus.get(i).get("RiskStatus"));
            resultList.add(temp);
        }

        return  resultList;
    }

    public static List<Map<String, Object>> BaseInfo(Map map){
        List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
        List<String> nodeList = new ArrayList<String>();
        nodeList.add("Respone>Body>ResultInfo>BaseInfo>RiskNum");
        nodeList.add("Respone>Body>ResultInfo>BaseInfo>SignCurrentNum");
        nodeList.add("Respone>Body>ResultInfo>BaseInfo>SignCurrentState");
        nodeList.add("Respone>Body>ResultInfo>BaseInfo>SignNum");
        nodeList.add("Respone>Body>ResultInfo>BaseInfo>BlackNum");
//        resultList=ParseXmlByPropertyFile.str2ListbyHandler(nodeList,map.get("retMsg").toString());
        return  resultList;
    }

    public static List<Map<String, Object>> HisSignList(Map map){
        List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
        List<Map> listmap= new ArrayList<>();


        List<String> Statuslist = new ArrayList<String>();
        LinkedList<Map<String, Object>> Status = new LinkedList<>();
        Statuslist.add("Respone>Body>ResultInfo>HisSignList>SignInfo>Status");
//        Status=ParseXmlByPropertyFile.str2ListbyHandler(Statuslist,map.get("retMsg").toString());

        List<String> OpenTypelist = new ArrayList<String>();
        LinkedList<Map<String, Object>> OpenType = new LinkedList<>();
        OpenTypelist.add("Respone>Body>ResultInfo>HisSignList>SignInfo>OpenType");
//        OpenType=ParseXmlByPropertyFile.str2ListbyHandler(OpenTypelist,map.get("retMsg").toString());

        List<String> EndTimelist = new ArrayList<String>();
        EndTimelist.add("Respone>Body>ResultInfo>HisSignList>SignInfo>EndTime");
        LinkedList<Map<String, Object>> EndTime = new LinkedList<>();
//        EndTime=ParseXmlByPropertyFile.str2ListbyHandler(EndTimelist,map.get("retMsg").toString());


        List<String> StartTimelist = new ArrayList<String>();
        LinkedList<Map<String, Object>> StartTime = new LinkedList<>();
        StartTimelist.add("Respone>Body>ResultInfo>HisSignList>SignInfo>StartTime");
//        StartTime=ParseXmlByPropertyFile.str2ListbyHandler(StartTimelist,map.get("retMsg").toString());

        List<String> OrgNamelist = new ArrayList<String>();
        OrgNamelist.add("Respone>Body>ResultInfo>HisSignList>SignInfo>OrgName");
        LinkedList<Map<String, Object>> OrgName = new LinkedList<>();
//        OrgName=ParseXmlByPropertyFile.str2ListbyHandler(OrgNamelist,map.get("retMsg").toString());


        List<String> RiskStatuslist = new ArrayList<String>();
        RiskStatuslist.add("Respone>Body>ResultInfo>HisSignList>SignInfo>RiskStatus");
        LinkedList<Map<String, Object>> RiskStatus = new LinkedList<>();
//        RiskStatus=ParseXmlByPropertyFile.str2ListbyHandler(RiskStatuslist,map.get("retMsg").toString());


        for (int i=0;i<Status.size();i++){
             Map temp = new HashMap();
             temp.put("Status",Status.get(i).get("Status"));
             temp.put("OpenType",OpenType.get(i).get("OpenType"));
             temp.put("EndTime",EndTime.get(i).get("EndTime"));
             temp.put("StartTime",StartTime.get(i).get("StartTime"));
             temp.put("OrgName",OrgName.get(i).get("OrgName"));
             temp.put("RiskStatus",RiskStatus.get(i).get("RiskStatus"));
             resultList.add(temp);
        }

        return  resultList;
    }

    public static List<Map<String, Object>> BlackList(Map map){
        List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
        List<Map> listmap= new ArrayList<>();


        List<String> DocCodelist = new ArrayList<String>();
        LinkedList<Map<String, Object>> DocCode = new LinkedList<>();
        DocCodelist.add("Respone>Body>ResultInfo>BlackList>RiskInfo>DocCode");
//        DocCode=ParseXmlByPropertyFile.str2ListbyHandler(DocCodelist,map.get("retMsg").toString());

        List<String> LegDocNamelist = new ArrayList<String>();
        LinkedList<Map<String, Object>> LegDocName = new LinkedList<>();
        LegDocNamelist.add("Respone>Body>ResultInfo>BlackList>RiskInfo>LegDocName");
//        LegDocName=ParseXmlByPropertyFile.str2ListbyHandler(LegDocNamelist,map.get("retMsg").toString());

        List<String> ValidStatuslist = new ArrayList<String>();
        ValidStatuslist.add("Respone>Body>ResultInfo>BlackList>RiskInfo>ValidStatus");
        LinkedList<Map<String, Object>> ValidStatus = new LinkedList<>();
//        ValidStatus=ParseXmlByPropertyFile.str2ListbyHandler(ValidStatuslist,map.get("retMsg").toString());


        List<String> CusNamelist = new ArrayList<String>();
        LinkedList<Map<String, Object>> CusName = new LinkedList<>();
        CusNamelist.add("Respone>Body>ResultInfo>BlackList>RiskInfo>CusName");
//        CusName=ParseXmlByPropertyFile.str2ListbyHandler(CusNamelist,map.get("retMsg").toString());

        List<String> Occurarealist = new ArrayList<String>();
        Occurarealist.add("Respone>Body>ResultInfo>BlackList>RiskInfo>Occurarea");
        LinkedList<Map<String, Object>> Occurarea = new LinkedList<>();
//        Occurarea=ParseXmlByPropertyFile.str2ListbyHandler(Occurarealist,map.get("retMsg").toString());


        List<String> ValidDatelist = new ArrayList<String>();
        ValidDatelist.add("Respone>Body>ResultInfo>BlackList>RiskInfo>ValidDate");
        LinkedList<Map<String, Object>> ValidDate = new LinkedList<>();
//        ValidDate=ParseXmlByPropertyFile.str2ListbyHandler(ValidDatelist,map.get("retMsg").toString());


        List<String> RegNamelist = new ArrayList<String>();
        LinkedList<Map<String, Object>> RegName = new LinkedList<>();
        RegNamelist.add("Respone>Body>ResultInfo>BlackList>RiskInfo>RegName");
//        RegName=ParseXmlByPropertyFile.str2ListbyHandler(RegNamelist,map.get("retMsg").toString());

        List<String> LegDocTypelist = new ArrayList<String>();
        LinkedList<Map<String, Object>> LegDocType = new LinkedList<>();
        LegDocTypelist.add("Respone>Body>ResultInfo>BlackList>RiskInfo>LegDocType");
//        LegDocType=ParseXmlByPropertyFile.str2ListbyHandler(LegDocTypelist,map.get("retMsg").toString());

        List<String> Levellist = new ArrayList<String>();
        Levellist.add("Respone>Body>ResultInfo>BlackList>RiskInfo>Level");
        LinkedList<Map<String, Object>> Level = new LinkedList<>();
//        Level=ParseXmlByPropertyFile.str2ListbyHandler(Levellist,map.get("retMsg").toString());


        List<String> LegDocCodelist = new ArrayList<String>();
        LinkedList<Map<String, Object>> LegDocCode =new LinkedList<>();
        LegDocCodelist.add("Respone>Body>ResultInfo>BlackList>RiskInfo>LegDocCode");
//        LegDocCode=ParseXmlByPropertyFile.str2ListbyHandler(LegDocCodelist,map.get("retMsg").toString());

        List<String> DocTypelist = new ArrayList<String>();
        DocTypelist.add("Respone>Body>ResultInfo>BlackList>RiskInfo>DocType");
        LinkedList<Map<String, Object>> DocType = new LinkedList<>();
//        DocType=ParseXmlByPropertyFile.str2ListbyHandler(DocTypelist,map.get("retMsg").toString());


        List<String> RiskTypelist = new ArrayList<String>();
        RiskTypelist.add("Respone>Body>ResultInfo>BlackList>RiskInfo>RiskType");
        LinkedList<Map<String, Object>> RiskType = new LinkedList<>();
//        RiskType=ParseXmlByPropertyFile.str2ListbyHandler(RiskTypelist,map.get("retMsg").toString());//CusType

        List<String> CusTypelist = new ArrayList<String>();
        CusTypelist.add("Respone>Body>ResultInfo>BlackList>RiskInfo>CusType");
        LinkedList<Map<String, Object>> CusType =  new LinkedList<>();
//        CusType=ParseXmlByPropertyFile.str2ListbyHandler(CusTypelist,map.get("retMsg").toString());//CusType

        for (int i=0;i<DocCode.size();i++){
            Map temp = new HashMap();
            temp.put("DocCode",DocCode.get(i).get("DocCode"));
            temp.put("LegDocName",LegDocName.get(i).get("LegDocName"));
            temp.put("ValidStatus",ValidStatus.get(i).get("ValidStatus"));
            temp.put("CusName",CusName.get(i).get("CusName"));
            temp.put("Occurarea",Occurarea.get(i).get("Occurarea"));
            temp.put("ValidDate",ValidDate.get(i).get("ValidDate"));

            temp.put("RegName",RegName.get(i).get("RegName"));
            temp.put("LegDocType",LegDocType.get(i).get("LegDocType"));
            temp.put("Level",Level.get(i).get("Level"));
            temp.put("LegDocCode",LegDocCode.get(i).get("LegDocCode"));
            temp.put("DocType",DocType.get(i).get("DocType"));
            temp.put("RiskType",RiskType.get(i).get("RiskType"));

            temp.put("CusType",CusType.get(i).get("CusType"));

            resultList.add(temp);
        }
        return  resultList;
    }



    public static List<Map<String, Object>> RespInfo(Map map){
        List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
        List<Map> listmap= new ArrayList<>();
        List<String> nodeList = new ArrayList<String>();
        nodeList.add("Respone>Body>RespInfo>ResultStatus");
        nodeList.add("Respone>Body>RespInfo>ResultCode");
//        resultList=ParseXmlByPropertyFile.str2ListbyHandler(nodeList,map.get("retMsg").toString());
        return  resultList;
    }

    public static List<Map<String, Object>> WarningList(Map map){
        List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
        List<Map> listmap= new ArrayList<>();


        List<String> DocCodelist = new ArrayList<String>();
        LinkedList<Map<String, Object>> DocCode = new LinkedList<>();
        DocCodelist.add("Respone>Body>ResultInfo>WarningList>RiskInfo>DocCode");
//        DocCode=ParseXmlByPropertyFile.str2ListbyHandler(DocCodelist,map.get("retMsg").toString());

        List<String> LegDocNamelist = new ArrayList<String>();
        LinkedList<Map<String, Object>> LegDocName = new LinkedList<>();
        LegDocNamelist.add("Respone>Body>ResultInfo>WarningList>RiskInfo>LegDocName");
//        LegDocName=ParseXmlByPropertyFile.str2ListbyHandler(LegDocNamelist,map.get("retMsg").toString());

        List<String> ValidStatuslist = new ArrayList<String>();
        ValidStatuslist.add("Respone>Body>ResultInfo>WarningList>RiskInfo>ValidStatus");
        LinkedList<Map<String, Object>> ValidStatus = new LinkedList<>();
//        ValidStatus=ParseXmlByPropertyFile.str2ListbyHandler(ValidStatuslist,map.get("retMsg").toString());


        List<String> CusNamelist = new ArrayList<String>();
        LinkedList<Map<String, Object>> CusName = new LinkedList<>();
        CusNamelist.add("Respone>Body>ResultInfo>WarningList>RiskInfo>CusName");
//        CusName=ParseXmlByPropertyFile.str2ListbyHandler(CusNamelist,map.get("retMsg").toString());

        List<String> Occurarealist = new ArrayList<String>();
        Occurarealist.add("Respone>Body>ResultInfo>WarningList>RiskInfo>Occurarea");
        LinkedList<Map<String, Object>> Occurarea = new LinkedList<>();
//        Occurarea=ParseXmlByPropertyFile.str2ListbyHandler(Occurarealist,map.get("retMsg").toString());


        List<String> ValidDatelist = new ArrayList<String>();
        ValidDatelist.add("Respone>Body>ResultInfo>WarningList>RiskInfo>ValidDate");
        LinkedList<Map<String, Object>> ValidDate = new LinkedList<>();
//        ValidDate=ParseXmlByPropertyFile.str2ListbyHandler(ValidDatelist,map.get("retMsg").toString());


        List<String> RegNamelist = new ArrayList<String>();
        LinkedList<Map<String, Object>> RegName = new LinkedList<>();
        RegNamelist.add("Respone>Body>ResultInfo>WarningList>RiskInfo>RegName");
//        RegName=ParseXmlByPropertyFile.str2ListbyHandler(RegNamelist,map.get("retMsg").toString());

        List<String> LegDocTypelist = new ArrayList<String>();
        LinkedList<Map<String, Object>> LegDocType = new LinkedList<>();
        LegDocTypelist.add("Respone>Body>ResultInfo>WarningList>RiskInfo>LegDocType");
//        LegDocType=ParseXmlByPropertyFile.str2ListbyHandler(LegDocTypelist,map.get("retMsg").toString());

        List<String> Levellist = new ArrayList<String>();
        Levellist.add("Respone>Body>ResultInfo>WarningList>RiskInfo>Level");
        LinkedList<Map<String, Object>> Level = new LinkedList<>();
//        Level=ParseXmlByPropertyFile.str2ListbyHandler(Levellist,map.get("retMsg").toString());


        List<String> LegDocCodelist = new ArrayList<String>();
        LinkedList<Map<String, Object>> LegDocCode =new LinkedList<>();
        LegDocCodelist.add("Respone>Body>ResultInfo>WarningList>RiskInfo>LegDocCode");
//        LegDocCode=ParseXmlByPropertyFile.str2ListbyHandler(LegDocCodelist,map.get("retMsg").toString());

        List<String> DocTypelist = new ArrayList<String>();
        DocTypelist.add("Respone>Body>ResultInfo>WarningList>RiskInfo>DocType");
        LinkedList<Map<String, Object>> DocType = new LinkedList<>();
//        DocType=ParseXmlByPropertyFile.str2ListbyHandler(DocTypelist,map.get("retMsg").toString());


        List<String> RiskTypelist = new ArrayList<String>();
        RiskTypelist.add("Respone>Body>ResultInfo>WarningList>RiskInfo>RiskType");
        LinkedList<Map<String, Object>> RiskType = new LinkedList<>();
//        RiskType=ParseXmlByPropertyFile.str2ListbyHandler(RiskTypelist,map.get("retMsg").toString());//CusType

        List<String> CusTypelist = new ArrayList<String>();
        CusTypelist.add("Respone>Body>ResultInfo>WarningList>RiskInfo>CusType");
        LinkedList<Map<String, Object>> CusType =  new LinkedList<>();
//        CusType=ParseXmlByPropertyFile.str2ListbyHandler(CusTypelist,map.get("retMsg").toString());//CusType

        for (int i=0;i<DocCode.size();i++){
            Map temp = new HashMap();
            temp.put("DocCode",DocCode.get(i).get("DocCode"));
            temp.put("LegDocName",LegDocName.get(i).get("LegDocName"));
            temp.put("ValidStatus",ValidStatus.get(i).get("ValidStatus"));
            temp.put("CusName",CusName.get(i).get("CusName"));
            temp.put("Occurarea",Occurarea.get(i).get("Occurarea"));
            temp.put("ValidDate",ValidDate.get(i).get("ValidDate"));

            temp.put("RegName",RegName.get(i).get("RegName"));
            temp.put("LegDocType",LegDocType.get(i).get("LegDocType"));
            temp.put("Level",Level.get(i).get("Level"));
            temp.put("LegDocCode",LegDocCode.get(i).get("LegDocCode"));
            temp.put("DocType",DocType.get(i).get("DocType"));
            temp.put("RiskType",RiskType.get(i).get("RiskType"));

            temp.put("CusType",CusType.get(i).get("CusType"));

            resultList.add(temp);
        }
        return  resultList;
    }
}

